﻿using System;
using Microsoft.EntityFrameworkCore;
using ZSZ.Application.Service;
using ZSZ.EntityFramework;

namespace ZSZ.Application
{
    public class UnitOfWork : IDisposable
    {
        private readonly ZSZDbContext _context = null;

        private AdminLogService _adminLogService = null;
        private AdminUserService _adminUserService = null;
        private RoleService _roleService = null;
        private CityService _cityService = null;
        private HouseAppointmentService _houseAppointmentService = null;
        private HouseService _houseService = null;
        private RegionService _regionService = null;
        private CommunityService _communityService = null;
        private IdNameService _idNameService = null;
        private AttachmentService _attachmentService = null;
        private PermissionService _permissionService = null;
        #region 配置上下文
        public UnitOfWork(ZSZDbContext context)
        {
            _context = context;
        }
        #endregion

        #region 注入service
        public AdminLogService AdminLogService
        {
            get { return _adminLogService ?? (_adminLogService = new AdminLogService(_context)); }
        }

        public AdminUserService AdminUserService
        {
            get { return _adminUserService ?? (_adminUserService = new AdminUserService(_context)); }
        }

        public RoleService RoleService
        {
            get { return _roleService ?? (_roleService = new RoleService(_context)); }
        }

        public CityService CityService
        {
            get { return _cityService ?? (_cityService = new CityService(_context)); }
        }

        public HouseAppointmentService HouseAppointmentService
        {
            get
            {
                return _houseAppointmentService ?? (_houseAppointmentService = new HouseAppointmentService(_context));
            }
        }

        public HouseService HouseService
        {
            get { return _houseService ?? (_houseService = new HouseService(_context)); }
        }

        public RegionService RegionService
        {
            get { return _regionService ?? (_regionService = new RegionService(_context)); }
        }

        public CommunityService CommunityService
        {
            get { return _communityService ?? (_communityService = new CommunityService(_context)); }
        }

        public IdNameService IdNameService
        {
            get { return _idNameService ?? (_idNameService = new IdNameService(_context)); }
        }

        public AttachmentService AttachmentService
        {
            get { return _attachmentService ?? (_attachmentService = new AttachmentService(_context)); }
        }

        public PermissionService PermissionService
        {
            get { return _permissionService ?? (_permissionService = new PermissionService(_context)); }
        }
        #endregion



        public bool SaveChange()
        {
            return _context.SaveChanges() > 0;
        }


        public void Dispose()
        {
            if (_context != null)
            {
                _context.Dispose();
            }
            GC.SuppressFinalize(this);
        }
    }
}